/*
    Copyright 2011 Peter Pratscher
 
    This file is part of Surveillance.NET.

    Surveillance.NET is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    Surveillance.NET is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Surveillance.NET. If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Net.Mail;
using System.Windows.Forms;
using SurveillanceDotNet.Model;
using System.Drawing;

namespace SurveillanceDotNet.AlarmActions
{
    public partial class FlaschScreenAlarmActionForm : Form
    {
        public FlashScreenAlarmAction Action { get; set; }

        private Database _Database;

        public FlaschScreenAlarmActionForm()
        {
            InitializeComponent();

            _Database = Database.Instance;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (tbxName.Text.Length == 0)
            {
                MessageBox.Show("Please enter a name for the action!", "Missing name", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (Action == null)
            {
                Action = new FlashScreenAlarmAction();
            }

            double flashTime;

            if (!double.TryParse(tbxFlashTime.Text, out flashTime))
            {
                MessageBox.Show("Please enter a flash period!", "Invalid flash period", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Action.FlashTime = flashTime * 60d * 1000d;

            Action.Name = tbxName.Text;
            Action.Code = tbxCode.Text;

            this.DialogResult = DialogResult.OK;
        }

        private void PlaySoundAlarmActionForm_Load(object sender, EventArgs e)
        {
            if (Action != null)
            {
                tbxName.Text = Action.Name;
                tbxCode.Text = Action.Code;
                tbxFlashTime.Text = (Action.FlashTime / 60d / 1000d).ToString();
                tbxColor1.BackColor = Action.FirstColor;
                tbxColor2.BackColor = Action.SecondColor;
                tbxColor3.BackColor = Action.ThirdColor;
            }
            else
            {
                int exitingActionCounter = 1;

                foreach (AlarmAction action in _Database.AlarmActions)
                {
                    if (action is FlashScreenAlarmAction) exitingActionCounter++;
                }

                tbxName.Text = "Flash Screen " + exitingActionCounter;

                tbxFlashTime.Text = "1";
                tbxColor1.BackColor = Color.Yellow;
                tbxColor2.BackColor = Color.Red;
                tbxColor3.BackColor = Color.Green;
            }
        }

        private void tbxColor1_Click(object sender, EventArgs e)
        {
            if (colorDialog.ShowDialog() == DialogResult.OK)
            {
                tbxColor1.BackColor = colorDialog.Color;
            }
        }

        private void tbxColor2_Click(object sender, EventArgs e)
        {
            if (colorDialog.ShowDialog() == DialogResult.OK)
            {
                tbxColor2.BackColor = colorDialog.Color;
            }
        }

        private void tbxColor3_Click(object sender, EventArgs e)
        {
            if (colorDialog.ShowDialog() == DialogResult.OK)
            {
                tbxColor3.BackColor = colorDialog.Color;
            }
        }
    }
}
